home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / cawf2st.zoo / string.c < prev   
C/C++ Source or Header  |  1992-04-12  |  7KB  |  229 lines

  1. /*
  2.  *      string.c - string support functions for cawf(1)
  3.  */
  4.  
  5. /*
  6.  *      Copyright (c) 1991 Purdue University Research Foundation,
  7.  *      West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *      University Computing Center.  Not derived from licensed software;
  11.  *      derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *      Permission is granted to anyone to use this software for any
  14.  *      purpose on any computer system, and to alter it and redistribute
  15.  *      it freely, subject to the following restrictions:
  16.  *
  17.  *      1. The author is not responsible for any consequences of use of
  18.  *         this software, even if they arise from flaws in it.
  19.  *
  20.  *      2. The origin of this software must not be misrepresented, either
  21.  *         by explicit claim or by omission.  Credits must appear in the
  22.  *         documentation.
  23.  *
  24.  *      3. Altered versions must be plainly marked as such, and must not
  25.  *         be misrepresented as being the original software.  Credits must
  26.  *         appear in the documentation.
  27.  *
  28.  *      4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32.  
  33.  
  34. /*
  35.  * Asmname(s, c) - assemble name
  36.  */
  37.  
  38. Asmname(s, c)
  39.         char *s;                        /* pointer to name */
  40.         char *c;                        /* code destination (c[3]) */
  41. {
  42.  
  43.         c[1] = c[2] = '\0';
  44.         while (*s && *s == ' ')
  45.                 s++;
  46.         if ((c[0] = *s) == '\0')
  47.                 return(0);
  48.         return(((c[1] = s[1]) == '\0') ? 1 : 2);
  49. }
  50.  
  51.  
  52. /*
  53.  * Delstr(sx) - delete string
  54.  */
  55.  
  56. Delstr(sx)
  57.         int sx;                         /* string index */
  58. {
  59.         char buf[MAXLINE];              /* message buffer */
  60.  
  61.         if (sx >= Nstr) {
  62.                 (void) sprintf(buf, " bad Delstr(%d) index", sx);
  63.                 Error(FATAL, LINE, buf, NULL);
  64.         }
  65.         Free(&Str[sx].str);
  66.         while (sx < (Nstr - 1)) {
  67.                 Str[sx] = Str[sx + 1];
  68.                 sx++;
  69.         }
  70.         Nstr--;
  71. }
  72.  
  73.  
  74. /*
  75.  * Findchar(nm, l, s, e) - find special character definition and
  76.  *                         optionally enter it
  77.  */
  78.  
  79. Findchar(nm, l, s, e)
  80.         char *nm;                       /* character name */
  81.         int l;                          /* effective length */
  82.         char *s;                        /* value string */
  83.         int e;                          /* 0 = find, don't enter
  84.                                          * 1 = don't find, enter */
  85. {
  86.         int cmp, hi, low, mid;
  87.         char c[3];
  88.  
  89.         c[0] = nm[0];
  90.         c[1] = (nm[1] == ' ' || nm[1] == '\t') ? '\0' : nm[1];
  91.         c[2] = '\0';
  92.         low = mid = 0;
  93.         hi = Nsch - 1;
  94.         while (low <= hi) {
  95.                 mid = (low + hi) / 2;
  96.                 if ((cmp = strncmp(c, Schar[mid].nm, 2)) < 0)
  97.                         hi = mid - 1;
  98.                 else if (cmp > 0)
  99.                         low = mid + 1;
  100.                 else {
  101.                         if ( ! e)
  102.                                 return(mid);
  103.                         Free(&Schar[mid].str);
  104.                         goto new_char;
  105.                 }
  106.         }
  107.         if ( ! e)
  108.                 return(-1);
  109.         if (Nsch >= MAXSCH)
  110.                 Error(FATAL, LINE, " at character table limit", NULL);
  111.         if (Nsch) {
  112.                 if (cmp > 0)
  113.                         mid++;
  114.                 for (hi = Nsch - 1; hi >= mid; hi--)
  115.                         Schar[hi+1] = Schar[hi];
  116.         }
  117.         Nsch++;
  118.         Schar[mid].nm[0] = c[0];
  119.         Schar[mid].nm[1] = c[1];
  120.  
  121. new_char:
  122.  
  123.         Schar[mid].str = Newstr(s);
  124.         Schar[mid].len = l;
  125.         return(mid);
  126. }
  127.  
  128.  
  129. /*
  130.  * Findhy(s, l, e) - find and optionally enter hyphen
  131.  */
  132.  
  133. Findhy(s, l, e)
  134.         char *s;                        /* value string */
  135.         int l;                          /* equivalent length */
  136.         int e;                          /* 0 = find, don't enter
  137.                                          * 1 = enter, don't find */
  138. {
  139.         int i;
  140.  
  141.         for (i = 0; i < Nhy; i++) {
  142.                 if (Font[0] == Hychar[i].font)
  143.                         break;
  144.         }
  145.         if (i >= Nhy) {
  146.                 if ( ! e)
  147.                         return(-1);
  148.                 if (Nhy >= MAXHYCH)
  149.                         Error(FATAL, LINE, " at hyphen limit for font ",
  150.                                 Font);
  151.                 Hychar[i].font = Font[0];
  152.                 Nhy++;
  153.         } else {
  154.                 if ( ! e)
  155.                         return(i);
  156.                 Error(WARN, LINE, " duplicate hyphen for font ", Font);
  157.                 Free(&Hychar[i].str);
  158.         }
  159.         Hychar[i].str = Newstr(s);
  160.         Hychar[i].len = l;
  161.         return(i);
  162. }
  163.  
  164.  
  165. /*
  166.  * Findstr(nm, s, e) - find and  optionally enter string in Str[]
  167.  */
  168.  
  169. char *
  170. Findstr(nm, s, e)
  171.         char *nm;                       /* 2 character string name */
  172.         char *s;                        /* string value */
  173.         int e;                          /* 0 = find, don't enter
  174.                                          * 1 = enter, don't find */
  175. {
  176.         char c[3];                      /* character buffer */
  177.         int cmp, hi, low, mid;          /* binary search controls */
  178.         int i;                          /* temporary indexes */
  179.         char *s1, *s2;                  /* temporary string pointers */
  180.  
  181.         c[0] = nm[0];
  182.         c[1] = (nm[1] == ' ' || nm[1] == '\t') ? '\0' : nm[1];
  183.         c[2] = '\0';
  184.         low = mid = 0;
  185.         hi = Nstr - 1;
  186.         Sx = -1;
  187.         while (low <= hi) {
  188.                 mid = (low + hi) / 2;
  189.                 if ((cmp = strncmp(c, Str[mid].nm, 2)) < 0)
  190.                         hi = mid - 1;
  191.                 else if (cmp > 0)
  192.                         low = mid + 1;
  193.                 else {
  194.                         Sx = mid;
  195.                         if ( ! e)
  196.                                 return(Str[mid].str);
  197.                         Free(&Str[mid].str);
  198.                         goto new_string;
  199.                 }
  200.         }
  201.         if ( ! e)
  202.                 return("");
  203.         if (Nstr >= MAXSTR)
  204.                 Error(FATAL, LINE, " out of space for string ", c);
  205.         if (Nstr) {
  206.                 if (cmp > 0)
  207.                         mid++;
  208.                 for (hi = Nstr - 1; hi >= mid; hi--)
  209.                         Str[hi+1] = Str[hi];
  210.         }
  211.         Nstr++;
  212.         Sx = mid;
  213.         Str[mid].nm[0] = c[0];
  214.         Str[mid].nm[1] = c[1];
  215.  
  216. new_string:
  217.  
  218.         if (s == NULL)
  219.                 return (Str[mid].str = Newstr(""));
  220.         i = (*s == '"') ? 1 : 0;
  221.         s1 = Str[mid].str = Newstr(s + i);
  222.         if (i) {
  223.                 s2 = s1 + strlen(s1);
  224.                 if (s2 > s1 && *(s2-1) == '"')
  225.                         *(s2-1) = '\0';
  226.         }
  227.         return(s1);
  228. }
  229.